package system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import common.controller.base.BaseController;
import common.model.po.SysRole;
import common.model.po.SysUser;
import common.model.request.base.PaginationRequest;
import common.model.response.base.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import system.model.request.SaveUserRequest;
import system.model.request.SearchUserRequest;
import system.model.request.UpdateUserValidRequest;
import system.service.SysRoleService;
import system.service.SysUserService;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户管理
 *
 * @author 米泽鹏
 * @since 2022-03-11
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/system/sys-user")
public class SysUserController extends BaseController {

	@Resource
	private SysUserService userService;
	@Resource
	private SysRoleService roleService;

	@ApiOperation(value = "用户分页查询", notes = "用户分页查询")
	@PostMapping("/readOnly/pageUser")
	public Result<Page<SysUser>> pageUser(@ApiParam(value = "分页查询请求", required = true) @RequestBody @Validated PaginationRequest<SearchUserRequest, SysUser> paginationRequest) {
		return successWithData(userService.pageUser(paginationRequest));
	}

	@ApiOperation(value = "用户新增或更新", notes = "用户新增或更新")
	@PostMapping("/saveUser")
	public Result<?> saveUser(@ApiParam(value = "用户新增或更新请求", required = true) @RequestBody @Validated SaveUserRequest request) {
		return userService.saveUser(request);
	}

	@ApiOperation(value = "用户删除", notes = "用户删除")
	@DeleteMapping("/deleteUser/{id}")
	public Result<?> deleteUser(@ApiParam(value = "用户id", required = true) @PathVariable Long id) {
		return userService.deleteUser(id);
	}

	@ApiOperation(value = "获取所有角色", notes = "获取所有角色")
	@PostMapping("/readOnly/getAllRole")
	public Result<List<SysRole>> getAllRole() {
		return successWithData(roleService.list());
	}

	@ApiOperation(value = "获取某个用户的所有角色", notes = "获取某个用户的所有角色")
	@GetMapping("/readOnly/getAllRoleByUserId/{userId}")
	public Result<List<SysRole>> getAllRoleByUserId(@ApiParam(value = "用户id", required = true) @PathVariable Long userId) {
		return successWithData(roleService.getRolesByUserId(userId));
	}

	@ApiOperation(value = "更新用户状态", notes = "更新用户状态", response = Result.class)
	@PutMapping("/updateUserValid")
	public Result<Boolean> updateUserValid(@ApiParam(value = "更新用户状态请求", required = true) @RequestBody @Validated UpdateUserValidRequest request) {
		return userService.updateUserValid(request);
	}

	@ApiOperation(value = "根据用户id清空用户数据", notes = "根据用户id清空用户数据", response = Result.class)
	@DeleteMapping("/clearUserData/{userId}")
	public Result<Boolean> clearUserData(@ApiParam(value = "用户id", required = true) @PathVariable Long userId) {
		return userService.clearUserData(userId);
	}

}
